package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.vo.UnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CommunityUnitMapper;
import com.ruoyi.system.domain.CommunityUnit;
import com.ruoyi.system.service.ICommunityUnitService;

/**
 * unitService业务层处理
 * 
 * @author hanpee
 * @date 2024-03-25
 */
@Service
public class CommunityUnitServiceImpl implements ICommunityUnitService 
{
    @Autowired
    private CommunityUnitMapper communityUnitMapper;

    /**
     * 查询unit
     * 
     * @param id unit主键
     * @return unit
     */
    @Override
    public CommunityUnit selectCommunityUnitById(Long id)
    {
        return communityUnitMapper.selectCommunityUnitById(id);
    }

    /**
     * 查询unit列表
     * 
     * @param communityUnit unit
     * @return unit
     */
    @Override
    public List<CommunityUnit> selectCommunityUnitList(CommunityUnit communityUnit)
    {
        return communityUnitMapper.selectCommunityUnitList(communityUnit);
    }

    /**
     * 新增unit
     * 
     * @param communityUnit unit
     * @return 结果
     */
    @Override
    public int insertCommunityUnit(CommunityUnit communityUnit) throws Exception {
        communityUnit.setCreateTime(DateUtils.getNowDate());
        // 此处为新增时自动为creator字段赋值，赋值的内容为当前登录用户的用户名
        communityUnit.setCreator(SecurityUtils.getUsername());

        UnitVo a = getByName(communityUnit.getName());
        if (a == null) {
            return communityUnitMapper.insertCommunityUnit(communityUnit);
        } else {
            String name = a.getName();
            Long buildingId = a.getBuildingId();
            try {
                if(Objects.equals(communityUnit.getBuildingId(), buildingId)) {
                    if (!communityUnit.getName().equals(name)) {
                        return communityUnitMapper.insertCommunityUnit(communityUnit);
                    } else {
                        throw new Exception();
                    }
                } else {
            if(buildingId.equals(communityUnit.getBuildingId())){
                if(!name.equals(communityUnit.getName())){
                    return communityUnitMapper.insertCommunityUnit(communityUnit);
                }else {
                    throw new Exception();
                }
            } else {
                return 0;
            }
                }
            } catch (Exception e) {
                throw new Exception(e);
            }
        }
    }

    /**
     * 修改unit
     * 
     * @param communityUnit unit
     * @return 结果
     */
    @Override
    public int updateCommunityUnit(CommunityUnit communityUnit)
    {
        communityUnit.setUpdateTime(DateUtils.getNowDate());
        return communityUnitMapper.updateCommunityUnit(communityUnit);
    }

    /**
     * 批量删除unit
     * 
     * @param ids 需要删除的unit主键
     * @return 结果
     */
    @Override
    public int deleteCommunityUnitByIds(Long[] ids)
    {
        return communityUnitMapper.deleteCommunityUnitByIds(ids);
    }

    /**
     * 删除unit信息
     * 
     * @param id unit主键
     * @return 结果
     */
    @Override
    public int deleteCommunityUnitById(Long id) throws Exception {
        boolean a = communityUnitMapper.watchParams(id);
        if (a) {
            return communityUnitMapper.deleteCommunityUnitById(id);
        } else {
            throw new Exception();
        }
    }

    /**
     * 单元名称重复校验
     * @param name 楼宇名称
     * @return unit类
     */
    @Override
    public UnitVo getByName(String name) {
        return communityUnitMapper.getByName(name);
    }

    /**
     * 查询单元
     * @return unit集合
     */
    @Override
    public List<CommunityUnit> listUnit() {
        return communityUnitMapper.listUnit();
    }

    /**
     * 级联删除
     * @param id 单元Id
     * @return boolean
     */
    @Override
    public boolean watchParams(Long id) {
        return communityUnitMapper.watchParams(id);
    }
}
